<?php
define('DU_DAY', 24*60*60);
define('DU_WEEK', 6*DU_DAY);


class DateUtilityComponent extends Object {
    
	var $offset = 0;
	var $format = 'Y-m-d';
	var $css_classes = array(
		'header'=>'daynames',
		'prevmonth'=>'prevmonth',
		'nextmonth'=>'nextmonth',
		'weekday'=>'weekday',
		'weekend'=>'weekend',
		'today'=>'weekday today'
		);
		
	function initialize(&$controller, $settings = array()) {
		$this->Controller =& $controller;
		$this->_set($settings);
	}
	
	function weekdayNames() {
		$weednames = array(
			array('short'=>__('Sun', true),'long'=>__('Sunday', true),'class'=>$this->css_classes['header']),
			array('short'=>__('Mon', true),'long'=>__('Monday', true),'class'=>$this->css_classes['header']),
			array('short'=>__('Tue', true),'long'=>__('Tuesday', true),'class'=>$this->css_classes['header']),
			array('short'=>__('Wed', true),'long'=>__('Wednesday', true),'class'=>$this->css_classes['header']),
			array('short'=>__('Thu', true),'long'=>__('Thursday', true),'class'=>$this->css_classes['header']),
			array('short'=>__('Fri', true),'long'=>__('Friday', true),'class'=>$this->css_classes['header']),
			array('short'=>__('Sat', true),'long'=>__('Saturday', true),'class'=>$this->css_classes['header'])
			);
			for($i = 0; $i<$this->offset; $i++) {
				$elm = array_shift($weednames);
				array_push($weednames, $elm);
			}
			return $weednames;
	}
	
	function numberOfWeeksInYear($year) { 
	    return date("W",mktime(0,0,0,12,28,$year)); 
	}
	
	function WeekToDate($week, $year) {
		$Jan1 = mktime (1, 1, 1, 1, 1, $year);
		$iYearFirstWeekNum = (int) strftime("%W",mktime (1, 1, 1, 1, 1, $year));
		if ($iYearFirstWeekNum == 1) {
			$week = $week - 1;
		}

		$weekdayJan1 = date ('w', $Jan1);
		$FirstMonday = strtotime(((4-$weekdayJan1)%7-3) . ' days', $Jan1);
		$CurrentMondayTS = strtotime(($week) . ' weeks', $FirstMonday);
		return ($CurrentMondayTS);
	}
	
	function dateRangeForMonth($year, $month, $full = true) {
		if($full) {
			$ts1 = mktime(0, 0, 0, $month, 1-$this->_firstDayOffsetForMonthView($year, $month), $year);
			$day1 = date($this->format, $ts1);
			$ts2 = mktime(0, 0, 0, $month+1, $this->_lastDayOffsetForMonthView($year, $month), $year);
			$day2 = date($this->format, $ts2);
		} else {
			$ts1 = mktime(0, 0, 0, $month, 1, $year);
			$day1 = date($this->format, $ts1);
			$ts2 = mktime(0, 0, 0, $month+1, 0, $year);
			$day2 = date($this->format, $ts2);
		}
		
		//debug(array($day1, $day2));
		return array(
			array('date'=> $day1,
			'ts'=>$ts1),
			array('date'=> $day2,
			'ts'=>$ts2)
		);
	}
	
	function dateRangeForWeek($year, $week) {
		$ts1 = $this->WeekToDate($week-1, $year);
		$day1 = date($this->format,$ts1);
		$ts2 = $ts1 + (DAY * 6);
		$day2 = date($this->format, $ts2);
		return array(
			array('date'=> $day1,
			'ts'=>$ts1),
			array('date'=> $day2,
			'ts'=>$ts2)
		);
	}
	
	function dateArrayForTsRange($ts1, $ts2) {
		// Switch if $ts2 is less that $ts1
		if($ts2<$ts1) {
			$tmp = $ts2;
			$ts2 = $ts1;
			$ts1 = $tmp;
			//debug('test');
		}
		$days = array();
		$days_in_range = ($ts2-$ts1)/DAY;
		$day1_weeknumber = $this->dayInWeek($ts1);
		$day2_weeknumber = $this->dayInWeek($ts2);
		//debug($day2_weeknumber);
		if($day1_weeknumber>0) {
			for($i = $day1_weeknumber; $i >= 0; $i--) {
				$ts = $ts1 - ($i*DAY);
				$days[date($this->format, $ts)] = $this->_dayForDateArray($ts, $this->css_classes['prevmonth'], 0);
			}
		}
		// Create dates for main month 
		for($i = 0; $i <= $days_in_range; $i++) {
			$ts = $ts1 + ($i*DAY);
			$days[date($this->format, $ts)] = $this->_dayForDateArray($ts, $this->css_classes['weekday'], 1, true);
		}
		$rest = 7-(count($days)%7);
		//debug($rest);
		// Create dates for later days in view 
		if($rest>0 and $rest != 7) {
			for($i = 1; $i <= $rest; $i++) {
				$ts = $ts2 + ($i*DAY);
				$days[date($this->format, $ts)] = $this->_dayForDateArray($ts, $this->css_classes['nextmonth'], 0);
			}
		}
		

		return $days;
	}
	
	function dateArrayForMonth($year, $month, $full = true) {
		$days = array();
		$days_in_month = date("t", mktime(0, 0, 0, $month, 1, $year));
		if($full) {
			$offset = $this->_firstDayOffsetForMonthView($year, $month);
			for($i = $offset-1; $i >= 0; $i--) {
				$ts = mktime(0, 0, 0, $month, -$i, $year);
				$days[date($this->format, $ts)] = $this->_dayForDateArray($ts, $this->css_classes['prevmonth'], 0);
			}
		}
		
		/* Create dates for main month */
		for($i = 1; $i <= $days_in_month; $i++) {
			$ts = mktime(0, 0, 0, $month, $i, $year);
			$days[date($this->format, $ts)] = $this->_dayForDateArray($ts, $this->css_classes['weekday'], 1, true);
		}
		if($full) {
			$rest = $this->_lastDayOffsetForMonthView($year, $month);
			/* Create dates for later days in view */
			for($i = 1; $i <= $rest; $i++) {
				$ts = mktime(0, 0, 0, $month+1, $i, $year);
				$days[date($this->format, $ts)] = $this->_dayForDateArray($ts, $this->css_classes['nextmonth'], 2);
			}
		}
		return $days;
	}
	
	function dayInWeek($ts) {
		$first_day = abs(date('w', $ts)+(6-$this->offset))+1;
		if($first_day>6) {
			$first_day = $first_day%7;
		}
		return $first_day;
	}
	function _firstDayOffsetForMonthView($year, $month) {
		$first_day = abs(date('w', mktime(0,0,0, $month, 1, $year))+(6-$this->offset))+1;
		if($first_day>6) {
			$first_day = $first_day%7;
		}
		return $first_day;
	}
	function _lastDayOffsetForMonthView($year, $month) {
		$days = $this->_firstDayOffsetForMonthView($year, $month);
		$days = $days + date("t", mktime(0, 0, 0, $month, 1, $year));
		$days = 7-($days%7);
		return $days;
	}
	function _dayForDateArray($ts, $classes, $status, $formatWeekdays = false) {
		if($formatWeekdays) {
			if(date('w', $ts) == 0 || date('w', $ts) == 6) {
				$classes = $this->css_classes['weekend'];
			} 
			if (date($this->format) == date($this->format, $ts)) {
				$classes = $this->css_classes['today'];
			}
		}
		return array(
			'date'=> date($this->format, $ts),
			'short'=> date('j', $ts),
			'class'=> $classes,
			'ts'=> $ts,
			'status'=>$status,
			'day_num'=>$this->dayInWeek($ts),
			'week_num'=> date('W', $ts),
			'month'=> date('m', $ts),
		);
	}
	
	
	
	
	
	function dateStringToUnix($date) {
		//debug($this);
		App::import("Helper", "Time");
    	$time = new TimeHelper();
		return $time->toUnix($date);
	}
	
}
?>